package com.cqc.retrofitdemo02;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.cqc.retrofitdemo02.event.AppEvent;
import com.cqc.retrofitdemo02.event.BaseEvent;
import com.cqc.retrofitdemo02.event.HttpErrorEvent;
import com.cqc.retrofitdemo02.event.HttpEvent;
import com.cqc.retrofitdemo02.event.HttpSuccessEvent;
import com.cqc.retrofitdemo02.event.RequestTag;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by cui on 2016/12/23.
 */

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBack(BaseEvent event) {
        if (event instanceof HttpSuccessEvent) {
            httpSuccessEvent((HttpSuccessEvent) event);
        } else if (event instanceof HttpErrorEvent) {
            httpErrorEvent((HttpErrorEvent) event);
        } else {
            appEvent((AppEvent) event);
        }
    }

    public abstract void appEvent(AppEvent event);

    public abstract void httpErrorEvent(HttpErrorEvent event);

    public abstract void httpSuccessEvent(HttpSuccessEvent event);

}
